//
//  LMRPDFTaskAlertView.m
//  ESignature_HD
//
//  Created by lhy on 2021/4/1.
//  Copyright © 2021 china-sss. All rights reserved.
//

#import "LMRPDFTaskAlertView.h"
#import "LHYDefine.h"
#import "LMRPDFTaskAlertViewCell.h"

@interface LMRPDFTaskAlertView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *btnOK;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation LMRPDFTaskAlertView

+ (instancetype)sharePDFTaskAlertView {
    LMRPDFTaskAlertView *view = [[NSBundle mainBundle] loadNibNamed:@"LMRPDFTaskAlertView" owner:nil options:nil].lastObject;
    view.frame = CGRectMake(50, 200, kScreenWidth - 100, 240);
    return view;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"LMRPDFTaskAlertViewCell" bundle:nil] forCellReuseIdentifier:@"LMRPDFTaskAlertViewCell"];
    
    [self.dataArr addObject:@"0"];
}

- (IBAction)btnOKClick:(UIButton *)sender {
    
    [self hide];
}

- (void)show {
    UIWindow *window = [[UIApplication sharedApplication] windows].firstObject;
    [UIView animateWithDuration:0.2 animations:^{
        [window addSubview:self];
        self.center = CGPointMake(window.center.x, window.center.y - 100);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [self removeFromSuperview];
}


#pragma mark - delegate / dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"LMRPDFTaskAlertViewCell";
    
    LMRPDFTaskAlertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LMRPDFTaskAlertViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell.btnAdd addTarget:self action:@selector(btnAddClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnDelete addTarget:self action:@selector(btnDeleteClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)btnAddClick:(UIButton *)sender {
    LMRPDFTaskAlertViewCell *cell = (LMRPDFTaskAlertViewCell *)sender.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    [self.dataArr insertObject:@"新添加" atIndex:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)btnDeleteClick:(UIButton *)sender {
    if (self.dataArr.count == 1) {
        return;
    }
    LMRPDFTaskAlertViewCell *cell = (LMRPDFTaskAlertViewCell *)sender.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    [self.dataArr removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


@end
